package com.l.web.master.system.group;

import com.l.web.WebConstant;
import org.apache.log4j.Logger;
import org.l.qiao.*;
import org.l.qiao.annotation.Privilege;
import org.l.qiao.util.ClusterUtil;
import com.l.web.util.ControllerSupport;
import org.l.qiao.web.KeyUpdate;
import com.l.web.common.ResponseCode;
import com.l.web.common.ResponseInfo;
import org.l.qiao.web.form.Form;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping(GroupController.URI)
public class GroupController extends ControllerSupport {
    public static final String URI = WebConstant.MASTER_URI + "/system/group";
    private static final Logger LOGGER = Logger.getLogger(GroupController.class);
    @Autowired
    private GroupService groupService;

    @Privilege(module = "用户", function = "组管理", operation = "查询")
    @RequestMapping("index.do")
    public ModelAndView index(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
        modelMap.put(WebConstant.CONTROLLER_KEY, URI);
        return new ModelAndView(URI + "/index", modelMap);
    }

    @Privilege(module = "用户", function = "组管理", operation = "查询")
    @RequestMapping("list.do")
    public ModelAndView list(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
        Form form = getForm(request);
        Page page = form.getPage();
        List<Cluster> list = groupService.getPageList(page);
        modelMap.put("list", list);
        modelMap.put("page", page);
        return new ModelAndView(URI + "/list", modelMap);
    }

    @Privilege(module = "用户", function = "组管理", operation = "详情")
    @RequestMapping("detail.do")
    public ModelAndView detail(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws IOException {
        Form form = getForm(request);
        String id = form.getString("id");

        if (Validate.check(Validate.OBJECTID, id)) {
            Group group = groupService.getDetail(id);

            if (group.isEmpty()) {
                LOGGER.error("所查看的组不存在");
                response.sendError(404);
                return null;
            }

            List<Cluster> users = groupService.getGroupUser(id);
            modelMap.put("group", group);
            modelMap.put("check", ClusterUtil.getCheck(Group.class));
            modelMap.put("users", users);
        } else {
            LOGGER.error("非法查看组信息：id=" + id);
            response.sendError(404);
            return null;
        }

        return new ModelAndView(URI + "/detail", modelMap);
    }

    @Privilege(module = "用户", function = "组管理", operation = "添加")
    @RequestMapping("add.do")
    public ModelAndView add(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
        modelMap.put("check", ClusterUtil.getCheck(Group.class));
        return new ModelAndView(URI + "/add", modelMap);
    }

    @Privilege(module = "用户", function = "组管理", operation = "添加")
    @RequestMapping("save.do")
    public void save(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws IOException {
        Form form = getForm(request);
        Group group = new Group();
        boolean result = form.getCluster(group);
        LOGGER.debug("添加组：" + group);

        if (result) {
            int code = groupService.save(group);
            ResponseInfo responseInfo = new ResponseInfo(code);
            responseInfo.put("id", group.getId());
            out(response, responseInfo);
        } else {
            response.sendError(404);
            LOGGER.error("非法添加组：" + group);
        }
    }

    @Privilege(module = "用户", function = "组管理", operation = "修改")
    @RequestMapping("update.do")
    public void update(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws IOException {
        Form form = getForm(request);
        KeyUpdate keyUpdate = form.getKeyUpdate(Group.class);
        LOGGER.debug("修改信息信息：" + keyUpdate);

        if (keyUpdate.check()) {
            int code = ResponseCode.FAILURE;

            try {
                code = groupService.update(keyUpdate);
            } catch (Exception e) {
                LOGGER.error("修改异常[" + keyUpdate + "]：" + e);
            }

            out(response, code);
        } else {
            LOGGER.error("非法修改：" + keyUpdate);
            response.sendError(404);
        }
    }

    @Privilege(module = "用户", function = "组管理", operation = "删除")
    @RequestMapping("remove.do")
    public void remove(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws IOException {
        Form form = getForm(request);
        List<String> ids = form.getList("id");
        LOGGER.debug("删除组：id=" + ids);

        if (Validate.check(Validate.OBJECTID, ids)) {
            int code = groupService.remove(ids);
            out(response, code);
        } else {
            LOGGER.error("非法删除组：id=" + ids);
            response.sendError(404);
        }
    }
}
